<script setup>
import { ref, watch, onMounted } from 'vue';
import { Search } from '@element-plus/icons-vue';

const props = defineProps({
  filters: {
    type: Object,
    required: true
  }
});

const emit = defineEmits(['filter-change']);

// 本地状态，用于UI界面
const localStatus = ref(props.filters.status || 'all');
const localKeyword = ref(props.filters.keyword || '');

// 状态选项
const statusOptions = [
  { label: '全部', value: 'all' },
  { label: '公开', value: 'approved' },
  { label: '隐藏', value: 'hidden' },
  { label: '审核', value: 'pending' },
  { label: '驳回', value: 'rejected' }
];

// 关键字搜索
const handleSearch = () => {
  emit('filter-change', { 
    keyword: localKeyword.value,
    status: localStatus.value
  });
};

// 状态变更
const handleStatusChange = () => {
  emit('filter-change', { 
    keyword: localKeyword.value,
    status: localStatus.value
  });
};

// 重置筛选
const resetFilters = () => {
  localStatus.value = 'all';
  localKeyword.value = '';
  emit('filter-change', { 
    keyword: '',
    status: 'all'
  });
};

// 监听外部状态变化
watch(() => props.filters, (newFilters) => {
  localStatus.value = newFilters.status || 'all';
  localKeyword.value = newFilters.keyword || '';
}, { deep: true });

// 输入框回车搜索
const handleKeyDown = (e) => {
  if (e.key === 'Enter') {
    handleSearch();
  }
};
</script>

<template>
  <div class="filter-container">
    <div class="filter-row">
      <!-- 状态筛选 -->
      <div class="filter-item status-filter">
        <el-radio-group v-model="localStatus" @change="handleStatusChange">
          <el-radio-button 
            v-for="option in statusOptions"
            :key="option.value"
            :value="option.value"
            :label="option.label"
          >
            {{ option.label }}
          </el-radio-button>
        </el-radio-group>
      </div>
      
      <!-- 搜索框 -->
      <div class="filter-item search-filter">
        <el-input
          v-model="localKeyword"
          placeholder="搜索视频标题"
          :prefix-icon="Search"
          clearable
          @keydown="handleKeyDown"
          @clear="handleSearch"
        >
          <template #append>
            <el-button @click="handleSearch">搜索</el-button>
          </template>
        </el-input>
      </div>
      
      <!-- 重置按钮 -->
      <div class="filter-item reset-filter">
        <el-button @click="resetFilters">重置</el-button>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.filter-container {
  background-color: #1F2129;
  padding: 16px;
  border-radius: 8px;
  margin-bottom: 16px;
}

.filter-row {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 16px;
}

.status-filter {
  flex-grow: 1;
}

.search-filter {
  width: 300px;
}

@media (max-width: 768px) {
  .filter-row {
    flex-direction: column;
    align-items: flex-start;
  }
  
  .search-filter {
    width: 100%;
  }
}
</style>